import matplotlib.pyplot as plt
import numpy as np
from keras.datasets import mnist
from keras.layers.core import Dense, Dropout
from keras.models import Sequential
from keras.utils import np_utils

(X_train, y_train), (X_test, y_test) = mnist.load_data()

print(X_train.shape, y_train.shape)
print(X_test.shape, y_test.shape)

X_train = X_train.reshape(X_train.shape[0], -1)  # 等价于X_train = X_train.reshape(60000,784)
X_test = X_test.reshape(X_test.shape[0], -1)  # 等价于X_test = X_test.reshape(10000,784)
X_train = X_train.astype("float32")
X_test = X_test.astype("float32")
X_train /= 255
X_test /= 255

y_train = np_utils.to_categorical(y_train, num_classes=10)
y_test = np_utils.to_categorical(y_test, num_classes=10)

model = Sequential()
model.add(Dense(512, input_shape=(784,), activation='relu'))
model.add(Dropout(0.2))
model.add(Dense(512, activation='relu'))
model.add(Dropout(0.2))
model.add(Dense(256, activation='relu'))
model.add(Dropout(0.2))
model.add(Dense(10, activation='softmax'))

model.compile(loss='categorical_crossentropy',
              optimizer='rmsprop',  # RMSprop()
              metrics=['accuracy'])

history = model.fit(X_train, y_train, epochs=10, batch_size=128,
                    verbose=1)

# score = model.evaluate(X_test, y_test)

#
# load(/test/test.csv)
# hanzi
# /

y_pre = model.predict(X_test)
print(np.argmax(y_pre))
# print(np.argmax(y_pre))
# print(np.argmax(y_test))

# print('score:', score[1])
#
# plt.plot(history.history['loss'])
# plt.plot(history.history['accuracy'])
# plt.legend(['loss', 'accuracy'])
# plt.show()
#
# 1. 格式化数据
# 2. 添加层
# 3. 编译模型
# 4. 训练模型
# 5. 评估/预测

